#include "ASFreePay.h"
#include "..\screen\ASPopupScreen.h"
#include "..\app\ASAppConst.h"

#ifdef AS_FREE_PAY_ENABLE_IN_PHONE

static ASBOOL s_as_free_pay_loadok;

static void as_free_pay_load_cb(ASINT result);

ASBOOL as_free_pay_if_load_ok(void)
{
    return s_as_free_pay_loadok;
}

static void as_free_pay_load_cb(ASINT result)
{
    as_log_app("<app>----remain mem %d,result is %d", mrc_getMemoryRemain(), result);

    if (MR_SUCCESS == result)
    {
        as_log_fp("as_free_pay_load_cb  ok");
        s_as_free_pay_loadok = ASTRUE;
    }
    else
    {
        as_log_fp("as_free_pay_load_cb  fail");
        s_as_free_pay_loadok = ASFALSE;
        //as_show_popup_screen(TEXT_FREE_PAY_LOAD_FAIL);
    }

    as_sync_internal_img_info();
    as_enter_loading_screen();
}

as_ret as_free_pay_point_check(ASINT point_idx, as_free_pay_result_cb result_cb)
{
    if(!result_cb)
        return AS_INVALID_PARAM;

    //if(!as_free_pay_if_load_ok())
    //{
    //    as_show_popup_screen(TEXT_FREE_PAY_WITHOUT_LOAD);
    //    return AS_INVALID_PARAM;
    //}
    as_log_fp("as_free_pay_point_check");

    if(mrc_freecurrency_reg_check(point_idx, (ChargeResultNotify)result_cb) == MR_SUCCESS)
        return AS_OK;
    else
        return AS_NOT_EXIST;
}

void as_free_pay_point_pay(ASINT point_idx, ASINT amount, ASSTR gb_brief, ASSTR gb_detail,
                           as_free_pay_result_cb result_cb)
{
    if(!gb_brief || !gb_detail || !result_cb)
        return;

    if(!as_free_pay_if_load_ok())
    {
        as_show_popup_screen(TEXT_FREE_PAY_WITHOUT_LOAD);
        return;
    }

    as_log_fp("as_free_pay_point_pay,[%d],[%d],%s---%s", point_idx, amount, gb_brief, gb_detail);
    as_screen_hide_adv_bar();
    mrc_freecurrency_reg_pay(point_idx, amount, (char*)as_ufg_tmp(0, gb_brief),
                             (char*)as_ucs2_from_gb2312(gb_detail), (ChargeResultNotify)result_cb);
}

void as_free_pay_prop_pay(ASINT prop_idx, ASINT amount, ASSTR gb_brief, ASSTR gb_detail,
                          as_free_pay_result_cb result_cb)
{
    if(!gb_brief || !gb_detail || !result_cb)
        return;

    if(!as_free_pay_if_load_ok())
    {
        as_show_popup_screen(TEXT_FREE_PAY_WITHOUT_LOAD);
        return;
    }

    as_screen_hide_adv_bar();
    mrc_freecurrency_prop_pay(prop_idx, amount, (char*)as_ufg_tmp(0, gb_brief),
                              (char*)as_ucs2_from_gb2312(gb_detail), (ChargeResultNotify)result_cb);
}

void as_free_pay_init(void)
{
    as_log_app("<app>----remain mem %d", mrc_getMemoryRemain());
    mrc_freecurrency_init(TRUE, FALSE, (PluginLoadCB)as_free_pay_load_cb);
    as_log_app("<app>----remain mem %d", mrc_getMemoryRemain());
}

void as_free_pay_deinit(void)
{
    if(s_as_free_pay_loadok)
    {
        mrc_freecurrency_unload();
        s_as_free_pay_loadok = ASFALSE;
    }
}

#endif//WIN32